package com.umerclass.ui.user;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umerclass.R;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.model.MsgBean;
import com.umerclass.frame.okhttp.OkSimpleRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.ui.anmo.CountDownTimer;
import com.umerclass.utils.Constants;
import com.umerclass.utils.EventConstants;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class UserLoginActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_one_phone)
    EditText etOnePhone;
    @BindView(R.id.et_one_password)
    EditText etOnePassword;
    @BindView(R.id.linear1)
    LinearLayout linear1;
    @BindView(R.id.et_two_phone)
    EditText etTwoPhone;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_two_code)
    EditText etTwoCode;
    @BindView(R.id.linear2)
    LinearLayout linear2;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.tv_password_login)
    TextView tvPasswordLogin;
    @BindView(R.id.linear3)
    LinearLayout linear3;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;
    @BindView(R.id.tv_login_kuai)
    TextView tvLoginKuai;
    @BindView(R.id.linear4)
    LinearLayout linear4;
    @BindView(R.id.linear_weixin)
    LinearLayout linearWeixin;
    @BindView(R.id.tv_yinshi)
    TextView tvYinshi;
    @BindView(R.id.tv_rules)
    TextView tvRules;
    @BindView(R.id.btn_login)
    Button btnLogin;
    private String phone1 = "";
    private String code1 = "";
    private int type = 1;//1验证码登录 2密码登录
    private String phone2 = "";
    private String password = "";
    private IWXAPI api;

    private boolean click = false;//是否可以点击获取验证码
    CountDownTimer timer1 = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvSendCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tvSendCode.setText("重新获取验证码");
        }
    };
    private boolean loginCode = false;
    private boolean loginPassword = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer1.cancel();
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_user_login;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        etTwoPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    phone1 = s.toString();
                    click = true;
                    tvSendCode.setBackgroundResource(R.drawable.shape_two_press);
                } else {
                    click = false;
                    phone1 = "";
                    tvSendCode.setBackgroundResource(R.drawable.shape_two_normal);
                }

                if(TextUtils.isEmpty(phone1) || TextUtils.isEmpty(code1)){
                    loginCode = false;
                    btnLogin.setBackgroundResource(R.drawable.shape_three_normal);
                }else {
                    btnLogin.setBackgroundResource(R.drawable.shape_two_press);
                    loginCode = true;
                }
            }
        });

        etTwoCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    code1 = s.toString();
                } else {
                    code1 = "";
                }

                if(TextUtils.isEmpty(phone1) || TextUtils.isEmpty(code1)){
                    loginCode = false;
                    btnLogin.setBackgroundResource(R.drawable.shape_three_normal);
                }else {
                    btnLogin.setBackgroundResource(R.drawable.shape_two_press);
                    loginCode = true;
                }
            }
        });

        etOnePhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    phone2 = s.toString();
                } else {
                    phone2 = "";
                }

                if(TextUtils.isEmpty(phone2) || TextUtils.isEmpty(password)){
                    loginPassword = false;
                    btnLogin.setBackgroundResource(R.drawable.shape_three_normal);
                }else {
                    btnLogin.setBackgroundResource(R.drawable.shape_two_press);
                    loginPassword = true;
                }
            }
        });

        etOnePassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    password = s.toString();
                } else {
                    password = "";
                }

                if(TextUtils.isEmpty(phone2) || TextUtils.isEmpty(password)){
                    loginPassword = false;
                    btnLogin.setBackgroundResource(R.drawable.shape_three_normal);
                }else {
                    btnLogin.setBackgroundResource(R.drawable.shape_two_press);
                    loginPassword = true;
                }
            }
        });
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back, R.id.tv_password_login, R.id.tv_code,
            R.id.tv_forget_password, R.id.tv_login_kuai, R.id.linear1, R.id.linear2, R.id.linear3,
            R.id.linear4, R.id.tv_yinshi, R.id.tv_rules,R.id.tv_send_code,R.id.btn_login,R.id.linear_weixin})
    public void OnClick(View view) {
        switch (view.getId()) {
            //微信登录
            case R.id.linear_weixin:
                if (isWeixinAvilible()) {  // 用户是否安装微信客户端
                    final SendAuth.Req req = new SendAuth.Req();
                     req.scope = "snsapi_userinfo";
                     req.state = "none";
                     api.sendReq(req);
                }else {
                    showToast("没有安装微信");
                }
                break;
            case R.id.btn_login:
                if(type == 1){

                }else if(type == 1){

                }
                break;
            //获取验证码
            case R.id.tv_send_code:
                if(click){
                    timer1.start();
                    OkSimpleRequest request = new OkSimpleRequest<>(R.id.authPhone,
                            Constants.authPhone);
                    request.addParams("phone", phone1);
                    requestOkhttpSimple(request);
                }
                break;
            //密码登录的
            case R.id.linear1:
                break;
            //验证码登录的
            case R.id.linear2:
                break;
            case R.id.linear3:
                break;
            case R.id.linear4:
                break;

            case R.id.linear_back:
                finish();
                break;
            //密码登录
            case R.id.tv_password_login:
                type = 2;
                linear1.setVisibility(View.VISIBLE);
                linear4.setVisibility(View.VISIBLE);
                linear2.setVisibility(View.GONE);
                linear3.setVisibility(View.GONE);
                tvSendCode.setText("获取验证码");
                tvSendCode.setBackgroundResource(R.drawable.shape_two_normal);
                timer1.cancel();
                btnLogin.setBackgroundResource(R.drawable.shape_three_normal);
                loginPassword = false;
                phone2 = "";
                password = "";
                etTwoPhone.setText("");
                etTwoCode.setText("'");
                etOnePhone.setText("");
                etOnePassword.setText("");
                etOnePhone.setHint("请输入11位手机号码");
                etOnePassword.setHint("请输入登录密码");
                break;
            //收不到验证码
            case R.id.tv_code:
                break;
            //忘记密码
            case R.id.tv_forget_password:
                break;
            //快速登录 验证码登录
            case R.id.tv_login_kuai:
                type = 1;
                linear1.setVisibility(View.GONE);
                linear4.setVisibility(View.GONE);
                linear2.setVisibility(View.VISIBLE);
                linear3.setVisibility(View.VISIBLE);
                click = false;
                loginCode = false;
                btnLogin.setBackgroundResource(R.drawable.shape_three_normal);
                phone1 = "";
                code1 = "";
                tvSendCode.setText("获取验证码");
                tvSendCode.setBackgroundResource(R.drawable.shape_two_normal);
                timer1.cancel();
                etTwoPhone.setText("");
                etTwoCode.setText("");
                etTwoPhone.setHint("请输入11位手机号码");
                etTwoCode.setHint("请输入验证码");
                break;
            //隐私政策
            case R.id.tv_yinshi:
                break;
            //未成年人保护规则
            case R.id.tv_rules:
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what) {
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private boolean isWXAppInstalledAndSupported() {
        IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
        msgApi.registerApp(Constants.APP_ID);
        boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled();
        return sIsWXAppInstalledAndSupported;
    }
    /**
     * 检测是否安装微信
     * @return
     */
    private boolean isWeixinAvilible() {
        final PackageManager packageManager = getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }
}
